<?php

class FlowerHuaController extends Common
{
    public $member;

    public function __construct()
    {
        parent::__construct();
        $this->member = $this->model('member');
    }

    public function indexAction()
    {
        $typeid = trim($this->get('typeid'))?trim($this->get('typeid')):trim($this->post('typeid'));
        $page = (int)$this->get('page') ? (int)$this->get('page') : 1;
        $pagesize = isset($this->site['SITE_ADMIN_PAGESIZE']) && $this->site['SITE_ADMIN_PAGESIZE'] ? $this->site['SITE_ADMIN_PAGESIZE'] : 8;
        $order = "id desc";
        $limit = ($page - 1) * $pagesize . ",$pagesize";
        $where = "1=1";
        if ($typeid) {
            $where .= " and typeid = $typeid";
            $this->view->assign('typeid', $typeid);
        }
        $urlparam = array('page' => '{page}','typeid'=>$typeid);
        $list = $this->dingdan->get_all_list($where, $order, $limit, true);
        $total = $this->dingdan->get_all_list($where, $order, $limit, false);
        $pagelist = $this->instance('pagelist');    //加载分页类
        $pagelist->loadconfig();
        $pagelist = $pagelist->total($total)->url(url('member/', $urlparam))->num($pagesize)->page($page)->output();
        $this->view->assign(array(
            'list' => $list,
            'page'=>$page,
            'total'=>$total,
            'pagelist' => $pagelist,
            'meta_title' => '首页',
        ));
        $this->view->display('index');
    }

    public function ajax_caidouAction()
    {
        $member_id = get_cookie('member_id');
        $typeid = $this->get('typeid');
        $page = (int)$this->get('page') ? (int)$this->get('page') : 1;
        $pagesize = isset($this->site['SITE_ADMIN_PAGESIZE']) && $this->site['SITE_ADMIN_PAGESIZE'] ? $this->site['SITE_ADMIN_PAGESIZE'] : 8;
        $order = "id desc";
        $urlparam = array('page' => '{page}', 'typeid' => $typeid);
        $where = "member_id=".$member_id;
        if($typeid){
            $where .=' and typeid='.$typeid;
            $this->view->assign('typeid',$typeid);
        }
        $limit = ($page - 1) * $pagesize . ",$pagesize";
        $total = $this->member->get_all_list($where, $order, $limit, true);
        $caidou = $this->member->get_all_list($where, $order, $limit);
        $pagelist = $this->instance('pagelist');    //加载分页类
        $pagelist->loadconfig();
        $pagelist = $pagelist->total($total)->url(url('member/caidou', $urlparam))->num($pagesize)->page($page)->output();

        if($this->isMobile()){
            $str = '';
            foreach ($caidou as $value) {
                $str .= "<li><div class='txt'><h3>{$value['description']}</h3><p>".date('Y-m-d H:i',$value[addtime])."</p></div><em>".($value['typeid']==1 ? "+" : "-")."{$value['number']}</em></li>";
            }
            if ($str == '') {
                $str .= "<p style='font-size:18px;padding:20px;' align='center'>暂时没有找到数据.</p>";
            }
            $arr['str'] = $str;
            $arr['page'] = str_replace('<li>共' . $total . '条</li>', '<li>共' . $total . '条</li>', $pagelist);
            echo json_encode($arr);
            die;
        }else{
            $str = '';
            foreach ($caidou as $value) {
                $str .= "<li><span class='ct'>" . date('y-m-d H:i:s', $value[addtime]) . "</span><span class='ct'>".($value['typeid']==1 ? "收入" : "支出")."</span><span class='ct'>{$value[number]}</span><span class='ct'>{$value[description]}</span></li>";
            }
            if ($str == '') {
                $str .= "<p style='font-size:18px;padding:20px;' align='center'>暂时没有找到数据.</p>";
            }
            $arr['str'] = $str;
            $arr['page'] = str_replace('<li>共' . $total . '条</li>', '<li>共' . $total . '条</li>', $pagelist);
            echo json_encode($arr);
            die;
        }

    }

    //    ajax获取城市信息
    public function ajax_getnation1Action()
    {
        $nationid = $this->get('id');
        $nation = $this->member->get_nation($nationid);
        $str = '';
        if ($nation) {
            $str = "<option value='0'>请选择种类</option>";
            foreach ($nation as $key => $value) {
                $str .= "<option value='" . $value['id'] . "'>" . $value['name'] . "</option>";
            }
        }
        $array['result'] = $str;
        echo json_encode($array);
        die;
    }

    public function ajax_delAction()
    {
        $id = $this->get('id');
        $status = $this->member->del($id);
        if ($status) {
            echo 1;
        } else {
            echo 2;
        }
    }
}